package cn.gson.crmdemo.controller.order.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.crmdemo.model.pojo.Address;
import cn.gson.crmdemo.model.pojo.Emp;
import cn.gson.crmdemo.model.pojo.Reimbursement;
import cn.gson.crmdemo.model.service.OrderService;
import cn.gson.crmdemo.model.service.OutorderService;
import cn.gson.crmdemo.model.service.ReturnService;
import cn.gson.crmdemo.model.vo.BxVo;
import cn.gson.crmdemo.model.vo.DeliverAddressCustomerOrderVo;
import cn.gson.crmdemo.model.vo.DeliverdetailedProductVo;
import cn.gson.crmdemo.model.vo.OrderAddressSalesopportunityVo;
import cn.gson.crmdemo.model.vo.OrderdetailedVo;
import cn.gson.crmdemo.model.vo.OutorderCustomerOrderVo;
import cn.gson.crmdemo.model.vo.OutorderdetailedProductVo;
import cn.gson.crmdemo.model.vo.ReturndetailedReturnProductVo;
import cn.gson.crmdemo.model.vo.ReturneCustomerOrderVo;

@Controller
public class OrderController {
	@Autowired
	OrderService os;
	@Autowired
	OutorderService outs;
	@Autowired
	ReturnService rs;
	
	/**
	 * 查询订单
	 * @return
	 */
	@RequestMapping("order-selectOrder")
	@ResponseBody
	public Map<String,Object> contractList(@RequestParam("page")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
		Page<Object> page=PageHelper.startPage(pageNum, pageSize);
		JSONArray list = os.selectOrder();
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("total", page.getTotal());
		return map;
	}
	
	
	/**
	 * 待处理订单
	 * @return
	 */
	@RequestMapping("order-selctOrderdcl")
	@ResponseBody
	public JSONArray selctOrderdcl() {
		return os.selctOrderdcl();
	}
	
	
	
	/**
	 * 添加/修改订单表
	 * @param hetong
	 * @return
	 */
	@RequestMapping("add-order")
	@ResponseBody
	public JSONArray addContract(OrderAddressSalesopportunityVo order) {
		os.addContract(order);
		int Oid = os.selectMaxOrderID();
		return os.selectQuotationWhereOrder(Oid);
	}
	
	
	
	/**
	 * 添加订单祥单
	 * @param out
	 */
	@RequestMapping("order-addOrderdetailed")
	public String addOrderdetailed(OrderdetailedVo orderdetailed) {
		os.addOrderdetailed(orderdetailed);
		return "redirect:order-selectOrder?page=1&pageSize=4";
	}
	
	
	
	
	/**
	 * id查询订单表
	 * @param a
	 * @return
	 */
	@RequestMapping("order-selectOrdersId")
	@ResponseBody
	public JSONArray selectOrder(int a) {
		JSONArray list = os.selectOrderID(a);
		return list;
	}
	
	
	/**
	 * 删除订单表
	 * @param a
	 * @return
	 */
	@RequestMapping("delete-Order")
	public String deleteOrder(int a) {
		os.deleteOrder(a);
		return "redirect:order-selectOrder?page=1&pageSize=4";
	}
	
	
	/**
	 * 查询最近添加的地址id
	 * @return
	 */
	@RequestMapping("address-selectAddressMaxId")
	@ResponseBody
	public JSONArray selectMaxAddressId() {
		return  os.selectMaxAddressId();
	}
	
	
	/**
	 * 添加/修改地址表
	 * @param hetong
	 * @return
	 */
	@RequestMapping("add-orderAddress")
	public String addAddress(Address address) {
		os.addAddress(address);
		return "redirect:address-selectAddressMaxId";
	}
	
	
	/**
	 * 查询销售机会
	 * @return
	 */
	@RequestMapping("order-selectSale")
	@ResponseBody
	public JSONArray selectSalesnities() {
		return os.selectSalesnities();
	}
	
	
	/**
	 * 查询地址联系人
	 * @return
	 */
	@RequestMapping("order-selectAddressName")
	@ResponseBody
	public JSONArray selectAddressName() {
		return os.selectAddressName();
	}
	
	
	/**
	 * 通过id查询信息
	 * @param a
	 * @return
	 */
	@RequestMapping("order-selectAddressID")
	@ResponseBody
	public JSONArray selectAddressID(int a) {
		return os.selectAddressID(a);
	}
	
	
	/**
	 * 修改订单审核状态
	 * @param state
	 * @return
	 */
	@RequestMapping("update-orderState")
	public String updateOrderState(int id,int state) {
		os.updateOrderState(id,state);
		return "redirect:order-selectOrder?page=1&pageSize=4";
	}
	
	
	
	/**
	 * 查询报价单
	 * @return
	 */
	@RequestMapping("order-selectQuotation")
	@ResponseBody
	public Map<String,Object> selectQuotation(@RequestParam("page")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
		Page<Object> page=PageHelper.startPage(pageNum, pageSize);
		JSONArray list = os.selectQuotation();
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("total", page.getTotal());
		return map;
	}
	
	
	
	/**
	 * 根据查询报价单
	 * @return
	 */
	@RequestMapping("order-selectQuotationwhereCustomer")
	@ResponseBody
	public Map<String,Object> selectQuotationwhereCustomer(@RequestParam("page")Integer pageNum,@RequestParam("pageSize")Integer pageSize,int customerid) {
		Page<Object> page=PageHelper.startPage(pageNum, pageSize);
		JSONArray list = os.selectQuotationwhereCustomer(customerid);
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("total", page.getTotal());
		return map;
	}
	
	
	
	
	/**
	 * 添加出库单
	 * @param out
	 */
	@RequestMapping("outorder-addOutorder")
	@ResponseBody
	public JSONArray addOutorder(OutorderCustomerOrderVo out) {
		outs.addOutorder(out);
		int a=os.selectMaxOutorderId();
		return outs.selectOutorderIdaddDetailed(a);
	}
	
	
	/**
	 * 添加出库祥单，并修改库存
	 * @param out
	 */
	@RequestMapping("outorder-addOutorderdetailed")
	public void addOutorderdetailed(OutorderdetailedProductVo out) {
		outs.addOutorderdetailed(out);
	}
	
	
	/**
	 * 修改发货状态已出库
	 * @param dstate
	 */
	@RequestMapping("update-updateDstate")
	public void updateDstate(int dstate) {
		os.updateDstate(dstate);
	}
	
	
	
	
	/**
	 * 添加发货单
	 * @param deliver
	 * @return
	 */
	@RequestMapping("add-Deliver")
	@ResponseBody
	public JSONArray addDeliver(DeliverAddressCustomerOrderVo deliver) {
		outs.addDeliver(deliver);
		int a=outs.selectMaxDeliverId();
		return outs.selectDeliverIdaddDetailed(a);
	}
	
	
	
	
	/**
	 * 添加发货祥单
	 * @param deliver
	 * @return
	 */
	@RequestMapping("add-Deliverdetailed")
	public String addDeliverdetailed(DeliverdetailedProductVo deliver) {
		outs.addDeliverdetailed(deliver);
		return "添加成功";
	}
	
	
	
	
	/**
	 * 修改订单状态已发货
	 * @param dstate
	 */
	@RequestMapping("update-updateDstateDeliver")
	public String updateDstateDeliver(int dstate) {
		outs.updateDstateDeliver(dstate);
		return "redirect:order-selectOrdersId?a="+dstate;
	}
	
	
	
	/**
	 * 添加报销单
	 * @param re
	 */
	@RequestMapping("Deliver-addReimbursement")
	public String addReimbursement(BxVo re) {
		try {
			outs.addReimbursement(re);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	
	
	/**
	 * 修改订单状态已退货
	 * @param dstate
	 */
	@RequestMapping("update-updateReturnState")
	public String updateReturnState(int dstate) {
		os.updateReturnState(dstate);
		return "redirect:order-selectOrdersId?a="+dstate;
	}
	
	
	
	

	/**
	 * 添加退货单
	 * @param hetong
	 * @return
	 */
	@RequestMapping("add-Return")
	@ResponseBody
	public JSONArray addReturn(ReturneCustomerOrderVo re) {
		rs.addReturn(re);
		int a= os.selectMaxReturnId();
		return os.selectMaxReturnIdaddDetailed(a);
	}
	
	
	
	
	/**
	 * 添加退货祥单
	 * @param deliver
	 * @return
	 */
	@RequestMapping("add-Returndetailed")
	public String addReturndetailed(ReturndetailedReturnProductVo deliver) {
		rs.addReturndetailed(deliver);
		return "添加成功";
	}
	
	
	
	/**
	 * 查询发货状态
	 * @return
	 */
	@RequestMapping("order-selectDstate")
	@ResponseBody
	public Map<String,Object> selectDstate(int state,@RequestParam("page")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
		Page<Object> page=PageHelper.startPage(pageNum, pageSize);
		JSONArray list = os.selectDstate(state);
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("total", page.getTotal());
		return map;
	}
	
	
	
	/**
	 * 模糊查询公司名称
	 * @return
	 */
	@RequestMapping("order-selectcompany")
	@ResponseBody
	public Map<String,Object> mhselectCompany(@RequestParam("page")Integer pageNum,@RequestParam("pageSize")Integer pageSize,String companyName) {
		Page<Object> page=PageHelper.startPage(pageNum, pageSize);
		JSONArray list = os.mhselectCompany("%"+companyName+"%");
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("total", page.getTotal());
		return map;
	}
	
	
	/**
	 * 获取登录人
	 * @param request
	 * @return
	 */
	@RequestMapping("getName")
	@ResponseBody
	public Emp getName(HttpServletRequest request) {
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		return emp;
	}
	
	
	/**
	 *生成随机数 时间+随机数
	 * @return
	 */
	@RequestMapping("generateRandom")
	@ResponseBody
	public String generateRandom() {
		return os.random();
	}
	
	
	
	/**
	 *修改订单发货状态为已收货5
	 * @return
	 */
	@RequestMapping("updateGoodsOk")
	public String updateGoodsOk(int dstate) {
		System.err.println(dstate);
		 os.updateGoodsOk(dstate);
		return "redirect:order-selectOrdersId?a="+dstate;
	}
}
